package com.gxc.authcore.system.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gxc.authcore.system.domain.Action;
import com.gxc.dbcore.dao.SqlDao;
import com.gxc.dbcore.service.BeanService;
import com.gxc.dbcore.util.CompareItem;
import com.gxc.dbcore.util.CompareType;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;

@Service
public class ActionService {

	@Autowired
	private SqlDao sqlDao;
	@Autowired
	private BeanService beanService;

	public Action retrieve(String id) {
		return beanService.get(sqlDao, Action.class, id);
	}

	@SuppressWarnings("rawtypes")
	public Action retrieveByMethod(String method) {
		List<CompareItem> conditionList = new ArrayList<CompareItem>();
		conditionList.add(new CompareItem("method", CompareType.EQUAL, method));
		List list = beanService.list(sqlDao, Action.class, conditionList, null);
		if (list != null && list.size() > 0) {
			return (Action) list.get(0);
		}
		return null;
	}

	public int create(Action action) {
		return beanService.create(sqlDao, action);
	}

	public int update(Action action) {
		return beanService.update(sqlDao, action);
	}

	public int delete(String id) {
		return beanService.delete(sqlDao, Action.class, id);
	}

	public int delete(String[] idArray) {
		return beanService.delete(sqlDao, Action.class, idArray);
	}

	public int clear() {
		return sqlDao.delete("system.action.clear");
	}

	@SuppressWarnings("rawtypes")
	public List list(Map params) {
		return beanService.list(sqlDao, Action.class, null, null);
	}

	@SuppressWarnings("rawtypes")
	public PageList listPage(Map params, PageInfo pageInfo) {
		return beanService.listPage(sqlDao, Action.class, null, null, pageInfo);
	}

	/**
	 * 根据角色id获取对应的action列表
	 * 
	 * @param role_id
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List getActionByRoleId(String role_id) {
		return sqlDao.selectList("system.action.getActionByRoleId", role_id);
	}

	/**
	 * 根据角色id获取对应的action url列表
	 * 
	 * @param role_id
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List getActionUrlByRoleId(String role_id) {
		return sqlDao.selectList("system.action.getActionUrlByRoleId", role_id);
	}

	/**
	 * 根据角色id获取对应的action method列表
	 * 
	 * @param role_id
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List getActionMethodByRoleId(String role_id) {
		return sqlDao.selectList("system.action.getActionMethodByRoleId",
				role_id);
	}

}
